package cn.stylefeng.guns.modular.transport.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.transport.entity.MapDisptch;
import cn.stylefeng.guns.modular.transport.entity.MapDisptchMongo;
import cn.stylefeng.guns.modular.transport.entity.MapDisptchNew;
import cn.stylefeng.guns.modular.transport.mapper.MapDisptchMapper;
import cn.stylefeng.guns.modular.transport.model.params.MapDisptchParam;
import cn.stylefeng.guns.modular.transport.model.result.MapDisptchResult;
import  cn.stylefeng.guns.modular.transport.service.MapDisptchService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wp
 * @since 2025-04-14
 */
@Service
public class MapDisptchServiceImpl extends ServiceImpl<MapDisptchMapper, MapDisptch> implements MapDisptchService {

    @Autowired
    private MapDisptchMapper mapDisptchMapper;


    @Override
    public void add(MapDisptchParam param){
        MapDisptch entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void insert(MapDisptch mapDisptch) {
        mapDisptchMapper.insertMapDisptch(mapDisptch);
    }

    @Override
    public void delete(MapDisptchParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(MapDisptchParam param){
        MapDisptch oldEntity = getOldEntity(param);
        MapDisptch newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public void updateMapDisptchById(MapDisptch mapDisptch) {
        mapDisptchMapper.updateMapDisptchById(mapDisptch);
    }

    @Override
    public MapDisptchResult findBySpec(MapDisptchParam param){
        return null;
    }

    @Override
    public List<MapDisptchResult> findListBySpec(MapDisptchParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(MapDisptchParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<MapDisptchNew> getMapDisptch(String dispatchNumber, String operateId) {
        return mapDisptchMapper.getMapDisptch(dispatchNumber, operateId);
    }

    @Override
    public List<MapDisptchMongo> getMapDisptchMongo(String dispatchNumber, String operateId) {
        return mapDisptchMapper.getMapDisptchMongo(dispatchNumber, operateId);
    }

    @Override
    public List<MapDisptchMongo> getMapDisptchMongoOnWay(String operateId) {
        return mapDisptchMapper.getMapDisptchMongoOnWay(operateId);
    }

    private Serializable getKey(MapDisptchParam param){
        return param.getDispatchNumber();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private MapDisptch getOldEntity(MapDisptchParam param) {
        return this.getById(getKey(param));
    }

    private MapDisptch getEntity(MapDisptchParam param) {
        MapDisptch entity = new MapDisptch();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
